<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <XForm ref="form" :fieldList="fieldList" :options="formOption" :formData="form" :originOption="originOption"></XForm>
      </template>

      <template #toolbar_btns>
        <el-button icon="Plus" type="primary" @click="query">查询</el-button>
      </template>

      <!-- 关联副店铺 -->
      <template #TableCell="{ row, column }">
        <el-link :underline="false" type="primary" @click="aaaaa(row, column)">{{ row[column.field] }}</el-link>
        <!-- <div @click="aaaaa(row, column)">{{ row[column.field] }}</div> -->
        <!-- <VText :value="item.sub_store_name" :line="1" v-if="i < 2" /> -->
      </template>
    </VTable>
  </VPage>
</template>

<script setup lang="jsx">
import { failedJobList, failedJobRetry, failedJobDelete, deliverStatistics } from "./api.js"
import { ElMessage, ElMessageBox } from "element-plus"
import { onMounted, ref, toRefs } from "vue"
import XForm from "@/components/XForm/index.vue"

const tableRef = ref()
// 获取已选中的行
const getSelectRow = () => {
  const getCheckboxRecords = tableRef?.value.$table.getCheckboxRecords()
  if (!getCheckboxRecords.length) {
    ElMessage({ showClose: true, message: "请至少选择一条数据！", type: "warning" })
    return false
  }
  return getCheckboxRecords?.map((v) => v.id)
}
import action from "@/utils/action.js"
const aaaaa = (row, column) => {
  console.log(row, column)

  // sessionStorage.setItem(
  //   "PURCHASE_HANDLE",
  //   JSON.stringify({
  //     sale_id: [row.sale_id],
  //     create_user_id: [row.create_user_id],
  //     // sku: res.data.sku,
  //     // sku_no: res.data.sku_no,
  //   })
  // )

  // console.log(tableRef?.value?.getForm())
  let data = tableRef?.value?.getForm()
  const tableForm = {
    sale_id: [row.sale_id],
    user: { type: "create_user_id", value: [row.create_user_id] },
    dateRange: { type: "deliver_time", value: [data.time?.[0] + " 00:00:00", data.time?.[1] + " 23:59:59"], range: true },
  }
  switch (column.field) {
    case "wait_sale_confirm_num":
      tableForm.plan_status = [2]
      break
    case "wait_order_num":
      tableForm.plan_status = [3]
      break
    case "part_order_num":
      tableForm.plan_status = [4]
      break
    case "order_num":
      tableForm.plan_status = [5]
      break

    default:
      break
  }
  // return
  // { field: "wait_sale_confirm_num", title: "待确认", slots: { default: "TableCell" } },
  // { field: "wait_order_num", title: "待建头程单", slots: { default: "TableCell" } },
  // { field: "part_order_num", title: "部分建头程单", slots: { default: "TableCell" } },
  // { field: "order_num", title: "已建头程单", slots: { default: "TableCell" } },

  // { field: "tc_wait_audit_num", title: "头程单待审核", slots: { default: "TableCell" } },
  // { field: "tc_wait_shipping_num", title: "头程单待发货", slots: { default: "TableCell" } },
  // { field: "tc_part_shipping_num", title: "头程单部分发货", slots: { default: "TableCell" } },
  // { field: "tc_shipping_num", title: "头程单已发货", slots: { default: "TableCell" } },
  // { field: "tc_part_return_num", title: "头程单部分退货", slots: { default: "TableCell" } },
  // { field: "tc_return_num", title: "头程单全部退货", slots: { default: "TableCell" } },

  sessionStorage.setItem("_table_form", JSON.stringify({ tableForm }))

  action.setGlobalState({
    changeMicoTabsPath: {
      path: "/purchase/demand/deliver",
      type: "push",
    },
  })
}

const formOption = ref({ inline: true })
// 搜索条件 配置
const publicStyle = { style: { width: "200px" } }
const fieldList = ref([
  {
    field: "time",
    type: "dateTimeRangeNorm",
    itemProps: { type: "daterange", format: "YYYY-MM-DD", valueFormat: "YYYY-MM-DD", clearable: false },
  },
  {
    field: "create_user_id",
    type: "cascaderNew",
    placeholder: "计划人员",
    ...publicStyle,
  },
  {
    field: "sale_id",
    type: "cascaderNew",
    placeholder: "运营人员",
    ...publicStyle,
  },
])

// 查询
const query = () => {
  tableRef?.value.query()
}
// 枚举 数据
const originOption = ref({})

import { shortcuts } from "@/components/XForm/config.js"
import { parseTime } from "@/utils/ruoyi"
//获取下拉数据
onMounted(async () => {
  let day = shortcuts.find((v) => v.text == "最近一个月")?.value()
  day = day.map((v) => parseTime(v, "{y}-{m}-{d}"))
  tableRef.value.setForm({ time: day })
})
// 列表配置
const tableOptins = reactive({
  formConfig: {
    data: {
      time: [],
    },
  },
  border: "",
  showOverflow: false,
  columns: [
    { field: "sale_name", title: "运营人员", width: 140 },
    { field: "create_user", title: "计划人员", width: 140 },
    {
      title: "发货计划",
      align: "center",
      children: [
        { field: "wait_sale_confirm_num", title: "待确认", slots: { default: "TableCell" } },
        { field: "wait_order_num", title: "待建头程单", slots: { default: "TableCell" } },
        { field: "part_order_num", title: "部分建头程单", slots: { default: "TableCell" } },
        { field: "order_num", title: "已建头程单", slots: { default: "TableCell" } },
      ],
    },

    {
      title: "头程备库单",
      align: "center",
      children: [
        { field: "tc_wait_audit_num", title: "头程单待审核" },
        { field: "tc_wait_shipping_num", title: "头程单待发货" },
        { field: "tc_part_shipping_num", title: "头程单部分发货" },
        { field: "tc_shipping_num", title: "头程单已发货" },
        { field: "tc_part_return_num", title: "头程单部分退货" },
        { field: "tc_return_num", title: "头程单全部退货" },
      ],
    },
  ],
  proxyConfig: {
    ajax: {
      query: ({ page, form }) => {
        let params = { ...form }
        if (form.time && form.time.length) {
          params.deliver_time_start = form.time[0]
          params.deliver_time_end = form.time[1]
        }
        delete form.time
        return deliverStatistics(params)
      },
    },
  },
})
</script>
<style lang="scss" scoped>
// :deep(.vxe-header--row:nth-child(2)) {
//   .vxe-header--column {
//     background: #eff3fe !important;
//   }
// }
</style>
